<template>
  <div
    class="app-container"
    v-loading="AllLoading"
    element-loading-text="资源包加载更新中(3-5分钟)，请勿离开此窗口"
    element-loading-spinner="el-icon-loading"
  >
    <div>
      <el-form :inline="true" :model="dataForm">
        <el-form-item>
          <el-button type="primary" @click="addBzbf(0)">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addZiyuan()">更新资源包</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="bzbfList"
      stripe
      v-loading="bzbfLoading"
      style="width: 100%"
    >
      <el-table-column
        prop="standardBrushName"
        header-align="center"
        align="center"
        label="视频名称"
      ></el-table-column>
      <el-table-column
        prop="sorder"
        header-align="center"
        align="center"
        label="顺序"
      ></el-table-column>
      <el-table-column
        prop="standardBrushVideo"
        header-align="center"
        align="center"
        label="标准笔法视频"
      >
        <template slot-scope="scope">
          <video
            :src="scope.row.standardBrushVideo"
            controls="controls"
            class="avatar"
          >
            您的浏览器不支持视频播放
          </video>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button style="" type="text" @click="addBzbf(1, scope.row.id)"
            >修改</el-button
          >
          <el-button type="text" @click="deleteById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <el-dialog
      :visible.sync="dialogBzbfVisible"
      :title="type == 0 ? '添加标准笔法视频' : '修改标准笔法视频'"
    >
      <el-form
        :rules="dataRule"
        ref="bzbfForm"
        :model="bzbf"
        label-width="120px"
      >
        <el-form-item
          label="视频名称"
          prop="standardBrushName"
          style="width: 300px"
        >
          <el-input v-model="bzbf.standardBrushName" />
        </el-form-item>
        <el-form-item label="顺序" prop="sorder">
          <el-input-number
            v-model="bzbf.sorder"
            controls-position="right"
            :min="1"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="标准笔法视频" prop="standardBrushVideo">
          <el-upload
            class="upload-uploader"
            :action="'http://121.41.167.195:8000/api/oss/upload'"
            :on-success="handleAudioSuccess"
            :before-upload="beforeVideoUpload"
            :show-file-list="false"
          >
            <video
              :src="bzbf.standardBrushVideo"
              controls="controls"
              class="avatar"
            >
              您的浏览器不支持视频频播放
            </video>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogBzbfVisible = false">取 消</el-button>
        <el-button
          :disabled="savebzbfBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Vue from "vue";
export default {
  data() {
    return {
      dataForm: { name: "" },
      buzhou: 0,
      bzbfLoading: false,
      AllLoading: false,
      saveBtnDisabled: false, // 保存按钮是否禁用
      bzbf: {
        // 出版社对象
        standardBrushName: "",
        standardBrushVideo: "",
        sorder: 1,
        id: null,
      },
      bzbfList: [],
      type: 0, // 0:添加1:修改
      xzBtn: false,
      dialogBzbfVisible: false,
      savebzbfBtnDisabled: false,
      dataRule: {
        standardBrushName: [
          { required: true, message: "视频名称不能为空", trigger: "blur" },
        ],
        sorder: [{ required: true, message: "顺序不能为空", trigger: "blur" }],
        standardBrushVideo: [{ required: true, message: "视频名称不能为空" }],
      },
      pageIndex: 1, // 当前页
      pageSize: 10, // 每页数据显示几条
      totalPage: 0, // 总记录数
    };
  },

  activated() {
    // 有数据就要回显数据
    this.getBzbfList();
  },

  methods: {
    beforeVideoUpload(file) {
      const fileName = file.name;
      const fileType = fileName.substring(fileName.lastIndexOf("."));
      if (fileType !== ".mp4" && fileType !== ".flv") {
        this.$message.error("上传视频频只能是mp4/flv!");
        return false;
      }
      return true;
    },
    //上传成功后的回调函数
    handleAudioSuccess(response) {
      this.bzbf.standardBrushVideo = response.url;
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getBzbfList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getBzbfList();
    },
    getBzbfList() {
      this.bzbfLoading = true;
      this.$http({
        url: this.$http.adornUrl(`/sf/cbstandardbrush/getBzbfList`),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.bzbfList = data.bzbfPage.records;
          this.totalPage = data.bzbfPage.total;
          this.xzBtn = true;
        } else {
          this.bzbfList = [];
          this.totalPage = 0;
        }
        this.bzbfLoading = false;
      });
    },
    // 章节对话框中的保存或修改
    saveOrUpdate() {
      if (this.type == 0) {
        // 添加章节
        this.bzbfFormSubmit();
      } else {
        // 修改章节
        this.updatebzbf();
      }
    },
    // 更新章节图片信息
    updatebzbf() {
      this.$refs["bzbfForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbstandardbrush/update`),
            method: "post",
            data: this.$http.adornData(
              {
                id: this.bzbf.id,
                sorder: this.bzbf.sorder,
                standardBrushName: this.bzbf.standardBrushName,
                standardBrushVideo: this.bzbf.standardBrushVideo,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getBzbfList();
                  this.dialogBzbfVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 打开对话框，添加、修改
    addBzbf(status, id) {
      // 重置表单 应该只是把表单有的清空而已
      this.$nextTick(() => {
        this.$refs.bzbfForm.resetFields();
      });
      if (status == 0) {
        // 添加
        this.type = 0;
        // 清空
        this.bzbf.id = null;
      } else {
        // 修改
        this.type = 1;
        // 回显数据
        this.$http({
          url: this.$http.adornUrl("/sf/cbstandardbrush/info/" + id),
          method: "get",
        }).then(({ data }) => {
          this.bzbf.id = data.cbStandardBrush.id;
          this.bzbf.sorder = data.cbStandardBrush.sorder;
          this.bzbf.standardBrushName = data.cbStandardBrush.standardBrushName;
          this.bzbf.standardBrushVideo =
            data.cbStandardBrush.standardBrushVideo;
        });
      }
      this.dialogBzbfVisible = true;
    },
    // 表单提交
    bzbfFormSubmit() {
      // 表单验证
      this.$refs["bzbfForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbstandardbrush/save`),
            method: "post",
            data: this.$http.adornData(
              {
                sorder: this.bzbf.sorder,
                standardBrushName: this.bzbf.standardBrushName,
                standardBrushVideo: this.bzbf.standardBrushVideo,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "添加成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  // 刷新
                  this.getBzbfList();
                  // 只能添加一次
                  this.xzBtn = true;
                  this.dialogBzbfVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 通过章节id删除章节
    deleteById(id) {
      this.$confirm(`确定对[标准笔法id=${id}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/sf/cbstandardbrush/delete"),
            method: "post",
            data: this.$http.adornData([id], false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getBzbfList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 更新资源包
    addZiyuan() {
      this.AllLoading = true;
      this.$http({
        url: this.$http.adornUrl("/sf/cbstandardbrush/createBzbfZip"),
        method: "get",
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "资源包更新成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.AllLoading = false;
            },
          });
        } else {
          this.$message.error("资源包更新失败");
          this.AllLoading = false;
        }
      });
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.chanpterList {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.chanpterList li {
  position: relative;
}
.chanpterList p {
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #ddd;
}
.chanpterList .acts {
  float: right;
  font-size: 14px;
}

.videoList {
  padding-left: 50px;
}
.videoList p {
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dotted #ddd;
}
</style>
